/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.summary.service.impl;

import me.zhengjie.modules.summary.domain.Summary;
import me.zhengjie.modules.utils.utils.DateUtils;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.summary.repository.SummaryRepository;
import me.zhengjie.modules.summary.service.SummaryService;
import me.zhengjie.modules.summary.service.dto.SummaryDto;
import me.zhengjie.modules.summary.service.dto.SummaryQueryCriteria;
import me.zhengjie.modules.summary.service.mapstruct.SummaryMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;

import java.text.SimpleDateFormat;
import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author yyj
* @date 2021-10-01
**/
@Service
@RequiredArgsConstructor
public class SummaryServiceImpl implements SummaryService {

    private final SummaryRepository summaryRepository;
    private final SummaryMapper summaryMapper;

    @Override

    public Map<String,Object> queryAll(SummaryQueryCriteria criteria, Pageable pageable){
        Page<Summary> page = summaryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(summaryMapper::toDto));
    }

    @Override
    public List<SummaryDto> queryAll(SummaryQueryCriteria criteria){
        return summaryMapper.toDto(summaryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public SummaryDto findById(Integer id) {
        Summary summary = summaryRepository.findById(id).orElseGet(Summary::new);
        ValidationUtil.isNull(summary.getId(),"Summary","id",id);
        return summaryMapper.toDto(summary);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SummaryDto create(Summary resources) {
        return summaryMapper.toDto(summaryRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Summary resources) {
        Summary summary = summaryRepository.findById(resources.getId()).orElseGet(Summary::new);
        ValidationUtil.isNull( summary.getId(),"Summary","id",resources.getId());
        summary.copy(resources);
        summaryRepository.save(summary);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(Integer[] ids) {
        summaryRepository.logicDel(ids);

//        for (Integer id : ids) {
//            summaryRepository.deleteById(id);
//        }
    }

    @Override
    public void download(List<SummaryDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SummaryDto summary : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", summary.getName());
            map.put("日期", summary.getDate());
            map.put("创建日期", summary.getCreateTime());
            map.put("删除标志", summary.getDelFlag());
            map.put("内容", summary.getContent());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public long summaryNum() {
        return summaryRepository.count();
    }




    public List<Map<String,Object>> summaryList(){
        Map<String,Object> map=new HashMap<>();
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :HH:mm:ss");

        List<Map<String,Object>>list = new ArrayList<>();
        map.put("date",dateFormat.format(date));
        list=summaryRepository.queryName();

        list.add(0,map);

        return list;
    }

    @Override
    public List<Summary> summaryContent(Integer id) {
        List<Summary> list= new ArrayList<>();
        list=summaryRepository.queryContent(id);
        return list;
    }

    @Override
    public long summaryKeyNum(String name) {
        return summaryRepository.summaryKeyNum(name);
    }


    public List<Map<Object, Object>> number() {
        long num=0;
        List <String> list = new ArrayList<>();
        List <Map<Object,Object>> dateList = new ArrayList<>();
        list= DateUtils.dateArr();
        for (int i = 0; i < list.size(); i++) {
            num=summaryRepository.number(list.get(i));
            Map<Object, Object> map = new HashMap<>();
            map.put("date",list.get(i));
            map.put("num",num);
            dateList.add(map);
        }
        return dateList;
    }

    public List<Map<Object, Object>> number(String dateNum) {
        long num=0;
        List <String> list = new ArrayList<>();
        List <Map<Object,Object>> dateList = new ArrayList<>();
        int k=Integer.parseInt(dateNum);
        list= DateUtils.dateArr(k);
        for (int i = 0; i < list.size(); i++) {
            num=summaryRepository.number(list.get(i));
            Map<Object, Object> map = new HashMap<>();
            map.put("date",list.get(i));
            map.put("num",num);
            dateList.add(map);
        }
        return dateList;
    }
}

